package com.fs.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.fs.domain.WorkerCertificationAudit;
import com.fs.mapper.AgencyProviderMapper;
import com.fs.domain.AgencyProvider;
import com.fs.service.IAgencyProviderService;

/**
 * 服务人员管理Service业务层处理
 * 
 * @author zh
 * @date 2024-10-22
 */
@Service
public class AgencyProviderServiceImpl implements IAgencyProviderService 
{
    @Autowired
    private AgencyProviderMapper agencyProviderMapper;

    /**
     * 查询服务人员管理
     * 
     * @param id 服务人员管理主键
     * @return 服务人员管理
     */
    @Override
    public AgencyProvider selectAgencyProviderById(Long id)
    {
        return agencyProviderMapper.selectAgencyProviderById(id);
    }

    /**
     * 查询服务人员管理列表
     * 
     * @param agencyProvider 服务人员管理
     * @return 服务人员管理
     */
    @Override
    public List<AgencyProvider> selectAgencyProviderList(AgencyProvider agencyProvider)
    {
        return agencyProviderMapper.selectAgencyProviderList(agencyProvider);
    }

    /**
     * 新增服务人员管理
     * 
     * @param agencyProvider 服务人员管理
     * @return 结果
     */
    @Transactional
    @Override
    public int insertAgencyProvider(AgencyProvider agencyProvider)
    {
        agencyProvider.setCreateTime(DateUtils.getNowDate());
        int rows = agencyProviderMapper.insertAgencyProvider(agencyProvider);
        insertWorkerCertificationAudit(agencyProvider);
        return rows;
    }

    /**
     * 修改服务人员管理
     * 
     * @param agencyProvider 服务人员管理
     * @return 结果
     */
    @Transactional
    @Override
    public int updateAgencyProvider(AgencyProvider agencyProvider)
    {
        agencyProvider.setUpdateTime(DateUtils.getNowDate());
        agencyProviderMapper.deleteWorkerCertificationAuditByServeProviderId(agencyProvider.getId());
        insertWorkerCertificationAudit(agencyProvider);
        return agencyProviderMapper.updateAgencyProvider(agencyProvider);
    }

    /**
     * 批量删除服务人员管理
     * 
     * @param ids 需要删除的服务人员管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteAgencyProviderByIds(Long[] ids)
    {
        agencyProviderMapper.deleteWorkerCertificationAuditByServeProviderIds(ids);
        return agencyProviderMapper.deleteAgencyProviderByIds(ids);
    }

    /**
     * 删除服务人员管理信息
     * 
     * @param id 服务人员管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteAgencyProviderById(Long id)
    {
        agencyProviderMapper.deleteWorkerCertificationAuditByServeProviderId(id);
        return agencyProviderMapper.deleteAgencyProviderById(id);
    }

    /**
     * 新增服务人员认证审核信息
     * 
     * @param agencyProvider 服务人员管理对象
     */
    public void insertWorkerCertificationAudit(AgencyProvider agencyProvider)
    {
        List<WorkerCertificationAudit> workerCertificationAuditList = agencyProvider.getWorkerCertificationAuditList();
        Long id = agencyProvider.getId();
        if (StringUtils.isNotNull(workerCertificationAuditList))
        {
            List<WorkerCertificationAudit> list = new ArrayList<WorkerCertificationAudit>();
            for (WorkerCertificationAudit workerCertificationAudit : workerCertificationAuditList)
            {
                workerCertificationAudit.setServeProviderId(id);
                list.add(workerCertificationAudit);
            }
            if (list.size() > 0)
            {
                agencyProviderMapper.batchWorkerCertificationAudit(list);
            }
        }
    }
}
